package com.our.sys.project1;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

import org.krysalis.barcode4j.HumanReadablePlacement;
import org.krysalis.barcode4j.impl.code128.Code128Bean;
import org.krysalis.barcode4j.output.bitmap.BitmapCanvasProvider;

public class CodeUtils {

    public static void generateCode128Barcode(File file, String code) {
        Code128Bean bean = new Code128Bean();
        final int dpi = 200;

        //barcode   
        bean.setModuleWidth(0.31);
        bean.setHeight(15);
        bean.doQuietZone(true);
        bean.setQuietZone(2);//两边空白区   
        //human-readable   
        bean.setFontName("Helvetica");
        bean.setFontSize(3);
        bean.setMsgPosition(HumanReadablePlacement.HRP_BOTTOM);

        OutputStream out = null;

        try {
            out = new FileOutputStream(file);

            BitmapCanvasProvider canvas = new BitmapCanvasProvider(out, "image/jpeg", dpi,
                    BufferedImage.TYPE_BYTE_BINARY, true, 0);
            bean.generateBarcode(canvas, code);
            canvas.finish();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (out != null)
                    out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }

}
